package com.hskn.hss.module.tlfacilitietype.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlfacilitietype.entity.TlFacilitiesType;
import com.hskn.hss.module.tlfacilitietype.mapper.TlFacilitiesTypeMapper;
import com.hskn.hss.module.tlfacilitietype.service.ITlFacilitiesTypeService;
import com.hskn.hss.module.tlfactypeitem.mapper.TlFacilitiesTypeItemsMapper;
import com.hskn.hss.module.tlthreshold.entity.TlThreshold;
import com.hskn.hss.module.tlthreshold.mapper.TlThresholdMapper;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.GroupingByUtils;
import com.hskn.hss.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-12
 */
@Service
public class TlFacilitiesTypeServiceImpl extends ServiceImpl<TlFacilitiesTypeMapper, TlFacilitiesType> implements ITlFacilitiesTypeService {

    @Resource
    TlFacilitiesTypeMapper tlFacilitiesTypeMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;
    @Resource
    TlFacilitiesTypeItemsMapper tlFacilitiesTypeItemsMapper;
    @Resource
    TlThresholdMapper tlThresholdMapper;

    @Override
    public AjaxResult insert(TlFacilitiesType tlFacilitiesType) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        tlFacilitiesType.setCreateby(uid);
        tlFacilitiesType.setCreatetime(new Date());
        tlFacilitiesType.setDeptid(deptid);
        int i = tlFacilitiesTypeMapper.insert(tlFacilitiesType);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlFacilitiesType tlFacilitiesType = params.getObject("data", TlFacilitiesType.class);
        tlFacilitiesType.setUpdateby(uid);
        tlFacilitiesType.setUpdatetime(new Date());
        int i = tlFacilitiesTypeMapper.updateById(tlFacilitiesType);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlFacilitiesTypeMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }
    @Override
    public AjaxResult deletebatchbyid(TlFacilitiesType tlFacilitiesType) throws HssException {
        String ids = tlFacilitiesType.getIds();
        String[] idArray = ids.split(",");
        tlFacilitiesTypeMapper.deleteBatchIds(Arrays.asList(idArray));
        return AjaxResult.success("操作成功");
    }
    @Override
    public TlFacilitiesType getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlFacilitiesType tlFacilitiesType = tlFacilitiesTypeMapper.selectById(id);
        return tlFacilitiesType;
    }

    @Override
    public List getSelectList(JSONObject params) throws HssException {
        Map<String, Object> pmap = new HashMap();
        pmap.put("type", params.getString("type"));
        if(StringUtils.isNotEmpty(params.getString("typeId"))){
            pmap.put("typeId", params.getString("typeId"));
        }
        List<Map> resultList = tlFacilitiesTypeMapper.getSelectList(pmap);
        for (Map typemap : resultList) {
            Map map = new HashMap();
            map.put("facilitietypeid", typemap.get("id"));
            List itemlist = tlFacilitiesTypeItemsMapper.getSelectList(map);
            typemap.put("items", itemlist);
        }
        return resultList;
    }

    @Override
    public JSONObject getList(JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map<String, Object> map = new HashMap();
        map.put("facilitietypename", params.getString("facilitietypename"));
        map.put("type", params.getString("type"));
        Page<TlFacilitiesType> listPage = new Page<>(page, size, true);
        IPage<TlFacilitiesType> iPage = tlFacilitiesTypeMapper.listTlFacilitiesType(listPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("factypelist", iPage.getRecords());
        return json;
    }
    @Override
    public Map<String, Object> getTreeForSheshiAlarm() {
        Map<String ,Object> map = new HashMap();
        QueryWrapper<TlThreshold> thresholdQueryWrapper=new QueryWrapper<>();
        thresholdQueryWrapper.lambda().eq(TlThreshold::getStatus,1);
        thresholdQueryWrapper.lambda().eq(TlThreshold::getThresholdtype,"facl");
        List<TlThreshold> tlThresholdList=tlThresholdMapper.selectList(thresholdQueryWrapper);
        QueryWrapper<TlFacilitiesType> tlFacilitiesTypeQueryWrapper= new QueryWrapper<TlFacilitiesType>();
        tlFacilitiesTypeQueryWrapper.lambda().eq(TlFacilitiesType::getType,"facl");
        List<TlFacilitiesType> facilitiesTypeList=tlFacilitiesTypeMapper.selectList(tlFacilitiesTypeQueryWrapper);
        Map<String ,List<TlThreshold>> threMap= GroupingByUtils.getGroupingForMapList(tlThresholdList,TlThreshold::getFaclitypeid);
        List<TlThreshold> thresholdList=new ArrayList<>();
        List<TlFacilitiesType> tlFacilitiesTypeList=new ArrayList<>();
        for(TlFacilitiesType facilitiesType:facilitiesTypeList){
            if(threMap.containsKey(facilitiesType.getId())){
                facilitiesType.getThresholdList().addAll(threMap.get(facilitiesType.getId()));
                thresholdList.addAll(threMap.get(facilitiesType.getId()));
                tlFacilitiesTypeList.add(facilitiesType);
            }
        }
        map.put("thresholdList",thresholdList);
        map.put("facilitiesTypeList",tlFacilitiesTypeList);
        return map;
    }
    @Override
    public Map<String,TlFacilitiesType> getTlFacilitiesTypeMap(QueryWrapper<TlFacilitiesType> facilitiesTypeQueryWrapper){
        Map<String,TlFacilitiesType> facilitiesTypeMap = new HashMap<>();
        List<TlFacilitiesType> facilitiesTypeList = tlFacilitiesTypeMapper.selectList(facilitiesTypeQueryWrapper);
        if(CollectionUtils.isNotEmpty(facilitiesTypeList)){
           facilitiesTypeMap = GroupingByUtils.getGroupingForMapEntity(facilitiesTypeList,TlFacilitiesType::getId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        return facilitiesTypeMap;
    }
}
